﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemSetup.InstitutionSetting.PeopleManagement;
using SystemCenterSoa.IRepository.SystemSetup.InstitutionSetting.PeopleManagement;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemSetup.InstitutionSetting.PeopleManagement;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSetup.InstitutionSetting.PeopleManagement
{
    public class PeopleRepository:BaseRepository,IPeople
    {
        public PeopleRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }
        /// <summary>
        /// 分页显示查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public PagerResult<PeopleDto> SearchPage(SearchModel searchModel)
        {
            var query = from peo in _systemCenterDbSoaContext.PeoplePo
                        select new PeopleDto
                        {
                            people_management_id = peo.people_management_id,
                            people_management_loginname = peo.people_management_loginname,
                            people_management_name = peo.people_management_name,
                            people_management_dataofbirth = peo.people_management_dataofbirth,
                            people_management_sex = peo.people_management_sex,
                            people_management_department_id = peo.people_management_department_id,
                            people_management_major_id = peo.people_management_major_id,
                            people_management_role_id = peo.people_management_role_id,
                            people_management_phonenumber = peo.people_management_phonenumber,
                            people_management_phone = peo.people_management_phone,
                            people_management_position = peo.people_management_position,
                            people_management_headportrait = peo.people_management_headportrait,
                            people_management_managerial_position_id = peo.people_management_managerial_position_id,
                            people_management_technology_position_id = peo.people_management_technology_position_id,
                            people_management_status = peo.people_management_status,
                            people_management_entry_time = peo.people_management_entry_time,
                            people_management_resignation_time = peo.people_management_resignation_time,
                            people_management_department_name = peo.people_management_department_name,
                            people_management_order = peo.people_management_order
                        };
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {//查询部门
                var tempDi = searchModel.SearchItem;
                string TrainDepartment = "people_management_department_id";
                if (tempDi.Keys.Contains(TrainDepartment) && !string.IsNullOrEmpty(tempDi[TrainDepartment]))
                {
                    int traindepar = -1;
                    if(int.TryParse(tempDi[TrainDepartment],out traindepar))
                    {
                        query = query.Where(p => p.people_management_department_id == traindepar);
                    }
                }
                //模糊查询姓名
                var TrainName = "people_management_name";
                if (tempDi.Keys.Contains(TrainName) && !string.IsNullOrEmpty(tempDi[TrainName]))
                {
                    query = query.Where(p => p.people_management_name.Contains(tempDi[TrainName]));
                }
                //状态查询
                var TrainStatus = "people_management_status";
                if (tempDi.Keys.Contains(TrainStatus) && !string.IsNullOrEmpty(tempDi[TrainStatus]))
                {
                    int trainstat = -1;
                    if(int.TryParse(tempDi[TrainStatus],out trainstat))
                    {
                        query = query.Where(p => p.people_management_status == trainstat);
                    }
                }
            }
            return BasePageSearch(query, searchModel);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool PostPeoAdd(PeoplePo m)
        {
            try
            {
                _systemCenterDbSoaContext.PeoplePo.Add(m);
                var yesno= _systemCenterDbSoaContext.SaveChanges();
                if (yesno > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 勾选删除
        /// </summary>
        /// <param name="strarr"></param>
        /// <returns></returns>
        public bool PostPenDeletes(string strarr)
        {
            try
            {
                var arr = strarr.Split(',');
                foreach (var item in arr)
                {
                    int id = int.Parse(item);
                    var only = _systemCenterDbSoaContext.PeoplePo.FirstOrDefault(p => p.people_management_id == id);
                    if (only != null)
                    {
                        _systemCenterDbSoaContext.PeoplePo.Remove(only);
                    }
                }
                var allover = _systemCenterDbSoaContext.SaveChanges();
                if (allover > 0)
                    return true;
                else
                    return false;
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PeoplePo GetFind(int id)
        {
            try
            {
                if(id== 0)
                    return null;
                return _systemCenterDbSoaContext.PeoplePo.FirstOrDefault(p => p.people_management_id == id);
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        public bool PostEdit(PeoplePo em)
        {
            try
            {
                if(em == null)
                    return false;
                _systemCenterDbSoaContext.Entry(em).State=Microsoft.EntityFrameworkCore.EntityState.Modified;
                var yesno = _systemCenterDbSoaContext.SaveChanges();
                if (yesno > 0)
                    return true;
                else
                    return false;
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        public List<DepartmentOfManagementPo> BinddepartmentSuperiorPos()
        {
            return _systemCenterDbSoaContext.DepartmentOfManagementPo.ToList();
        }
    }
}
